﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASCalpuff.FileLayers
{
    /// <summary>
    /// 字符串辅助写工具
    /// </summary>
    public class StrWriter
    {


        /// <summary>
        /// 返回！key=val！\r\n的字符串
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>

        public static string GetKeyValStr(string key, string val)
        {

            return "! " + key + " = " + val + " !\r\n";

        }


        /// <summary>
        /// 返回*key=  *\r\n的字符串
        /// </summary>
        /// <returns></returns>
        public static string GetNullKeyValStr(string key)
        {
            return "* " + key + " = " + "" + " *\r\n";
        }



        /// <summary>
        /// 返回---------------------------\r\n的字符串
        /// </summary>
        /// <returns></returns>
        public static string GetCLine()
        {
            return "-------------------------------------------------------------------------------\r\n";

        }
        /// <summary>
        /// 返回一个Sring的 List 集合
        /// </summary>
        /// <param name="str">需要分割的字符串</param>
        /// <returns></returns>
        public static List<string> ReturnStrList(string str)
        {
            List<string> lstS = new List<string>();
            List<int> lstI = new List<int>();
            string ss = ",";
            int p = -1;
            if (str != "" & str != null)
            {
                foreach (char s in str)
                {

                    p += 1;
                    if (Convert.ToString(s) == ",")
                    {
                        lstI.Add(p);

                    }
                }
                for (int i = 0; i < lstI.Count; i++)
                {
                    if (i == 0)
                    {

                        ss = str.Substring(0, lstI[i]);
                    }
                    else
                    {

                        ss = str.Substring(lstI[i - 1] + 1, lstI[i] - lstI[i - 1] - 1);

                    }
                    lstS.Add(ss);

                }
                if (lstI[lstI.Count - 1] < str.Length - 1)
                {
                    int a = lstI[lstI.Count - 1] + 1;
                    int b = lstI[lstI.Count - 1];
                    int c = str.Length - b;
                    ss = str.Substring(lstI[lstI.Count - 1] + 1, str.Length - lstI[lstI.Count - 1] - 1);
                    lstS.Add(ss);
                }
            }

            return lstS;
        }
        /// <summary>
        /// 返回一个double的 List 集合
        /// </summary>
        /// <param name="str">需要分割的字符串</param>
        /// <returns></returns>
        public static List<double> ReturnDouList(string str)
        {
            List<double> lstS = new List<double>();
            List<int> lstI = new List<int>();
            double ss = -1;
            int p = -1;
            if (str != "" & str != null)
            {

                foreach (char s in str)
                {

                    p += 1;
                    if (Convert.ToString(s) == ",")
                    {
                        lstI.Add(p);

                    }
                }
                for (int i = 0; i < lstI.Count; i++)
                {
                    if (i == 0)
                    {

                        ss = Convert.ToDouble(str.Substring(0, lstI[i]));
                    }
                    else
                    {

                        ss = Convert.ToDouble(str.Substring(lstI[i - 1] + 1, lstI[i] - lstI[i - 1] - 1));

                    }
                    lstS.Add(ss);

                }
                if (lstI[lstI.Count - 1] < str.Length - 1)
                {
                    int a = lstI[lstI.Count - 1] + 1;
                    int b = lstI[lstI.Count - 1];
                    int c = str.Length - b;
                    ss = Convert.ToDouble(str.Substring(lstI[lstI.Count - 1] + 1, str.Length - lstI[lstI.Count - 1] - 1));
                    lstS.Add(ss);
                }
            }

            return lstS;
        }
        /// <summary>
        /// 返回一个Int的 List 集合
        /// </summary>
        /// <param name="str">需要分割的字符串</param>
        /// <returns></returns>
        public static List<int> ReturnIntList(string str)
        {
            List<int> lstS = new List<int>();
            List<int> lstI = new List<int>();
            int ss = -1;
            int p = -1;

            if (str != "" & str != null)
            {
                foreach (char s in str)
                {

                    p += 1;
                    if (Convert.ToString(s) == ",")
                    {
                        lstI.Add(p);

                    }
                }
                for (int i = 0; i < lstI.Count; i++)
                {
                    if (i == 0)
                    {

                        ss = Convert.ToInt32(str.Substring(0, lstI[i]));
                    }
                    else
                    {

                        ss = Convert.ToInt32(str.Substring(lstI[i - 1] + 1, lstI[i] - lstI[i - 1] - 1));

                    }
                    lstS.Add(ss);

                }
                if (lstI[lstI.Count - 1] < str.Length - 1)
                {
                    int a = lstI[lstI.Count - 1] + 1;
                    int b = lstI[lstI.Count - 1];
                    int c = str.Length - b;
                    ss = Convert.ToInt32(str.Substring(lstI[lstI.Count - 1] + 1, str.Length - lstI[lstI.Count - 1] - 1));
                    lstS.Add(ss);
                }
            }

            return lstS;
        }

        public static string RemoveChar(string str)
        {
            int index = -1;
            //index = str.IndexOf(",");
            //str = str.Remove(index, 1);
            //index  = -1;
            //index = str.IndexOf(",");
            //if (index != -1)
            //{
            //    StrWriter.RemoveChar(str);
            //}
            //return str;
            for (int i = 0; i < str.Length; i++)
            {
                //int index = -1;
                index = str.IndexOf(",");
                if (index != -1)
                {
                    str = str.Remove(index, 1);
                }
                index = -1;
                index = str.IndexOf(",");
                if (index == -1)
                {
                    break;
                }
            }
            return str;
        }
        public static string GetString(string str, int index)
        {
            str = StrWriter.RemoveChar(str);

            str = "  " + str + "  ";//前后各增加两个空格，以便于正确处理
            string substring = "";
            int n = 0;
            int n1 = 0;
            int n2 = 0;
            try
            {

                for (int i = 0; i < str.Length - 1; i++)
                {
                    if (str.Substring(i, 1) != " ")
                    {
                        n1 = i;
                        n2 = str.IndexOf(" ", n1);
                        substring = str.Substring(n1, n2 - n1);
                        i = n2;
                        n = n + 1;
                        if (n == index)
                        {
                            return substring;

                        }
                    }
                }

            }
            catch
            {
                return null;
            }
            return null;
        }
        public static bool ReturnBool(string str, string s)
        {
            int b = -1;
            b = str.IndexOf(s);
            if (b != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="index">1 = cpuff ,2 = cmet</param>
        /// <returns></returns>
        public static string StrSubstitute(string str, int index)
        {
            string s = "";
            if (index == 1)
            {
                List<string> lstr = new List<string> { "cpufffilename0", "cpuffrunparam1", "cpufftechoption2", "cpuffspecieslist3", "cpuffgrid4", "cpuffoutputoption5", "cpuffsubgridetc6", "cpuffdrydepostion7", "cpuffdryparticle8", "cpuffdrymisc9", "cpuffwetdeposition10", "cpuffchemistryparam11", "cpuffdispersionandcomputationalparam12", "cpuffpoints13", "cpuffareas14", "cpufflines15", "cpuffvolumes16", "cpuffdiscrete17" };
                List<string> ls = new List<string> { "GROUP:0", "GROUP:1", "GROUP:2", "GROUP:3a,", "GROUP:4", "GROUP:5", "GROUP:6a", "GROUP:7", "GROUP:8", "GROUP:9", "GROUP:10", "GROUP:11", "GROUP:12", "GROUPS:13a", "GROUPS:14a", "GROUPS:15a", "GROUPS:16a", "GROUPS:17a", };
                for (int i = 0; i < ls.Count; i++)
                {
                    if (i < 13)
                    {
                        if (str.Substring(0, 7) == ls[i].Substring(0, 7))
                        {
                            s = lstr[i];
                            break;

                        }
                    }
                    else
                    {
                        if (str.Substring(0, 9) == ls[i].Substring(0, 9))
                        {
                            s = lstr[i];
                            break;

                        }
                    }

                }
            }
            else if (index == 2)
            {
                List<string> lstr = new List<string> { "cmetdata0", "cmetcontrol1", "cmetgrid2", "cmetoutputoption3", "cmetdataoption4", "cmetwind5", "cmetmixingtemperaturepricipitation6", "cmetsufstation7", "cmetupairstation8", "cmetpricipitation9" };
                List<string> ls = new List<string> { "GROUP:0", "GROUP:1", "GROUP:2", "GROUP:3,", "GROUP:4", "GROUP:5", "GROUP:6", "GROUP:7", "GROUP:8", "GROUP:9" };
                for (int i = 0; i < ls.Count; i++)
                {
                    if (str.Substring(0, 7) == ls[i].Substring(0, 7))
                    {
                        s = lstr[i];
                        break;

                    }
                }

            }
            return s;
        }



    }
}
